/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package com.tonbeller.jpivot.mondrian;

import com.tonbeller.jpivot.core.ExtensionSupport;

import com.tonbeller.jpivot.olap.model.Cell;
import com.tonbeller.jpivot.olap.navi.DrillThrough;

import com.tonbeller.tbutils.res.Resources;

import com.tonbeller.wcf.table.TableModel;

import mondrian.olap.Util.PropertyList;

import mondrian.rolap.RolapConnectionProperties;


/**
 * DOCUMENT ME!
 *
 * @author Robin Bagot  Implementation of the DrillExpand Extension for Mondrian Data Source.
 */
public class MondrianDrillThrough extends ExtensionSupport implements DrillThrough {
	private boolean extendedContext = true;

/**
   * Constructor sets ID
   */
	public MondrianDrillThrough() {
		super.setId(DrillThrough.ID);
	}

	/**
	 * drill through is possible if <code>member</code> is not calculated
	 *
	 * @param cell the cell.
	 *
	 * @return true if can drillthrough, false otherwise.
	 */
	public boolean canDrillThrough(Cell cell) {
		return ((MondrianCell)cell).getMonCell().canDrillThrough();

		//String sql = ((MondrianCell) cell).getMonCell().getDrillThroughSQL(extendedContext);
		//return sql != null;
	}

	/**
	 * does a drill through, retrieves data that makes up the selected Cell
	 *
	 * @param cell the cell.
	 *
	 * @return table model
	 *
	 * @throws NullPointerException if cannot retrieve the SQL.
	 */
	public TableModel drillThrough(Cell cell) {
		String sql = ((MondrianCell)cell).getMonCell().getDrillThroughSQL(extendedContext);

		if(sql == null) {
			throw new NullPointerException("DrillThroughSQL returned null");
		}

		MondrianDrillThroughTableModel dtm = new MondrianDrillThroughTableModel();

		dtm.setSql(sql);

		MondrianModel theModel = (MondrianModel)getModel();

		PropertyList connectInfo = theModel.getConnectProperties();
		String jdbcUrl = connectInfo.get(RolapConnectionProperties.Jdbc.name());
		String jdbcUser = connectInfo.get(RolapConnectionProperties.JdbcUser.name());
		String jdbcPassword = connectInfo.get(RolapConnectionProperties.JdbcPassword.name());
		String dataSourceName = connectInfo.get(RolapConnectionProperties.DataSource.name());

		dtm.setJdbcUrl(jdbcUrl);
		dtm.setJdbcUser(jdbcUser);
		dtm.setJdbcPassword(jdbcPassword);
		dtm.setDataSourceName(dataSourceName);
		dtm.setExternalDataSource(theModel.getExternalDataSource());

		Resources resources = Resources.instance(theModel.getLocale(), MondrianDrillThrough.class);

		dtm.setResources(resources);

		Resources drillThrough = Resources.instance(theModel.getLocale(), "drillthrough");

		dtm.setDrillThroughResources(drillThrough);


		return dtm;
	}

	/**
	 * Check if it is a extended context.
	 *
	 * @return true if extended, false otherwise.
	 */
	public boolean isExtendedContext() {
		return extendedContext;
	}

	/**
	 * Sets the extended context flag.
	 *
	 * @param extendedContext the extended context flag.
	 */
	public void setExtendedContext(boolean extendedContext) {
		this.extendedContext = extendedContext;
	}
}
